package com.fancy.learncenter.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.DataAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.DataCategoryDataBean;
import com.fancy.learncenter.bean.TopNewsDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.PopupWindowMaterrial;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MaterialDataActivity extends BaseActivity {

    String category_id = "";
    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @Bind(R.id.prompt_img)
    ImageView promptImg;
    @Bind(R.id.rootView)
    RelativeLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_data, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("素材资料");
        category_id = getIntent().getStringExtra("category_id");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        newsAdapter = new DataAdapter(this, new ArrayList<TopNewsDataBean>());
        recycleView.setAdapter(newsAdapter);

        newsAdapter.setCustomLoadMoreView(new CustomFootView(this));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getAccount("", true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getAccount("", false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });


        getAccount(category_id, true);

//        popupWindowMaterrial = new PopupWindowMaterrial(this, rootView);
//        popupWindowMaterrial.setOnCallBack(new PopupWindowMaterrial.OnCallBack() {
//            @Override
//            public void onCallBack(DataCategoryDataBean.ListBean items) {
//                getAccount(items.getId(), true);
//            }
//
//        });
    }


    PopupWindowMaterrial popupWindowMaterrial;

    DataAdapter newsAdapter;

    private void getAccount(String categoryId, final boolean isRefresh) {
        Map maps = new HashMap();

        maps.put("categoryId", category_id);
        HttpMehtod.getInstance().dataLists(maps, new IdeaObserver<BaseDataBean<List<TopNewsDataBean>>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<List<TopNewsDataBean>> listBaseDataBean) {

                if (listBaseDataBean.getData() != null) {
                    newsAdapter.notifyDataSetChanged((ArrayList<TopNewsDataBean>) listBaseDataBean.getData());
                } else {
                    newsAdapter.notifyDataSetChanged(new ArrayList<TopNewsDataBean>());
                }

                if (isRefresh) {

                    if (xRefreshView == null) {
                        return;
                    }
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                    if (listBaseDataBean.getData() == null) {
                        promptImg.setVisibility(View.VISIBLE);
                        newsAdapter.notifyDataSetChanged(new ArrayList());
                        return;
                    } else {
                        promptImg.setVisibility(View.GONE);
                        newsAdapter.notifyDataSetChanged((ArrayList<TopNewsDataBean>) listBaseDataBean.getData());
                    }

                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    newsAdapter.addAll(listBaseDataBean.getData());
                }


            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }

        });
    }

    @OnClick(R.id.type_image)
    public void onViewClicked() {
        popupWindowMaterrial.showPopupWindow();
    }
}
